<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

//接单
class Orderbuy extends Controller
{
    //接单者全部订单
    public function getbuylist()
    {
        $uid = Session::get('uid');
        $page = input('page', 1);
        $pagesax = input('pagesax', 4);
        $status = input('status');
        $where['buy_id'] = $uid;
        $where['a.valid'] = 1;
        if ($status != null) {
            $where['status'] = $status;
        }
        $res = Db::name('order_info')->alias('a')->join('bill_info b', 'a.b_id = b.id')->field('a.*,b.face_amt,b.expirydate,b.req_name,b.company_type')->where($where)->order('a.created_time desc')->select();
        $view = page_array($pagesax, $page, $res, 1);
        if ($view['data'] == null) {
            return msg(1, '暂无数据', $view);
            exit();
        } else {
            return msg(1, '接单者全部订单', $view);
            exit();
        }
    }


    //接受订单
    public function getbuy()
    {
        //$uid = Session::get('uid');
        $uid = 1;
        $bill_no = input('bill_no');
        //print_r($bill_no);exit();
        if ($bill_no == null) {
            return msg(1, '缺少重要参数', $bill_no);
            exit();
        }

        $order_sn = 'S' . date("YmdHis") . time();
        $billwhere['bill_no'] = $bill_no;
        $billinfo = Db::name('bill_info')->where($billwhere)->find();
        //生成订单编号 查询数据库判断是否重复
        $order_sn_get = Db::name('order_info')->where(['order_sn' => $order_sn])->find();
        $userwhere['id'] = $uid;
        $userinfo = Db::name('user')->where($userwhere)->find();
        if ($order_sn_get == null) {
            //支付保证金
            $userwhere['id'] = $uid;
            $money = $userinfo['money'] * 1 - $billinfo['sure_charge'] * 1;
            $userinforesdata['money'] = $money;
            $userinfores = Db::name('user')->where($userwhere)->update($userinforesdata);
            $data['order_sn'] = $order_sn;
            $data['sell_id'] = $billinfo['uid'];
            $data['buy_id'] = $uid;
            $data['service_charge'] = $billinfo['sure_charge'];
            $data['status'] = 3;
            $data['created_time'] = time();
            $data['bill_no'] = $billinfo['bill_no'];
            $data['valid'] = 1;
            $data['b_id'] = $billinfo['id'];
            $data['is_pay'] = 2;
            $data['paytimeout'] = strtotime('+21 day');
            $orderinsert = Db::name('order_info')->insertGetId($data);
            if ($orderinsert) {
                //改变票据状态
                Db::name('bill_info')->where($billwhere)->update(['status' => 1]);
                $this->addbondlog($billinfo['sure_charge'], $uid, $orderinsert, $bill_no, 3);
                return msg(1, '接单成功', $orderinsert);
                exit();
            } else {
                return msg(1, '接单失败请联系管理员', $orderinsert);
                exit();
            }
        }
    }

    public function addbondlog($bond, $uid, $orderid, $bill_no, $status)
    {
        //添加扣除记录
        $bondlog['create_time'] = time();
        $bondlog['title'] = '支付订单保证金';
        $bondlog['money'] = $bond;
        $bondlog['uid'] = $uid;
        $bondlog['orderid'] = $orderid;
        $bondlog['type'] = 1;
        $bondlog['bill_no'] = $bill_no;
        $bondlog['status'] = $status;
        Db::name('bond_log')->insert($bondlog);
    }


    //确认订单成功
    public function orderok()
    {
        $uid = Session::get('uid');
        $id = input('id');
        if ($id == null) {
            return msg(2, '缺少主要参数', $id);
            exit();
        }
        $where['id'] = $id;
        $billdata['status'] = 3;
        Db::name('bill_info')->where($where)->update($billdata);

        $data['status'] = 4;//确认完成
        $data['complete_time'] = time();
        $orderinfo = Db::name('order_info')->where($where)->update($data);
        if ($orderinfo) {
            return msg(1, '确认完成', $orderinfo);
            exit();
        } else {
            return msg(2, '确认完成失败', $orderinfo);
            exit();

        }
    }

    //取消交易
    public function notbuy()
    {
        $uid = Session::get('uid');
        $id = input('id');
        if ($id == null) {
            return msg(2, '缺少主要参数', $id);
            exit();
        }
        $where['id'] = $id;
        $data['cancel_time'] = time();
        $data['status'] = 4;
        $res = Db::name('order_info')->where($where)->update($data);
        $info = Db::name('order_info')->where($where)->find();
        if ($res) {
            //更改票据状态
            $blllist['status'] = 2;
            Db::name('bill_info')->where('id', $info['b_id'])->update($blllist);
            //返还保证金
            $fhbonddata['money'] = $info['service_charge'] * 0.1 * 0.75;
            Db::name('user')->where('id', $uid)->update($fhbonddata);
            return msg(1, '取消交易成功', $res);
            exit();
        } else {
            return msg(2, '取消交易失败', $res);
            exit();
        }
    }
    //1待接单 2支付服务费 3支付保证金 4 服务完成 5买家确认 6卖家确认完成 7取消交易 8 交易超时
    //2待卖家支付服务费、3服务中、4、取消订单(交易)  1服务完成、-1交易失败（超时）0 待接单  5确认完成

    //确认完成
    public function buyok()
    {
        $uid = Session::get('uid');
        $id = input('id');
        if ($id == null) {
            return msg(2, '缺少主要参数', $id);
            exit();
        }
        $where['id'] = $id;
        $data['complete_time'] = time();
        $data['status'] = 1;
        $res = Db::name('order_info')->where($where)->update($data);
        $info = Db::name('order_info')->where($where)->find();
        if ($res) {
            //更改票据状态
            $blllist['status'] = 1;
            Db::name('bill_info')->where('id', $info['b_id'])->update($blllist);
            return msg(1, '确认完成成功', $res);
            exit();
        } else {
            return msg(2, '确认完成失败', $res);
            exit();
        }
    }


}